package com.premnirmal.magnet.library;

import com.premnirmal.magnet.ResourceTable;
import cs.help.tool.Log;
import ohos.agp.animation.Animator;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

import java.math.BigDecimal;

import static com.premnirmal.magnet.library.Magnet.getDisplayHightInPx;
import static com.premnirmal.magnet.library.Magnet.getDisplayWidthInPx;
import static ohos.agp.components.element.ShapeElement.LINEAR_GRADIENT_SHADER_TYPE;

/**
 * Created by prem on 7/20/14.
 * ViewHolder for the remove Icon.
 */
public class RemoveView {

    private ComponentContainer layout;
    protected Component button;
    protected Component shadow;
    protected Text showLocation;
    protected Image buttonImage;
    protected WindowManager windowManager;
    protected SimpleAnimator showAnim;
    protected SimpleAnimator hideAnim;
    protected Context context;

    protected SimpleAnimator shadowFadeOut;
    protected SimpleAnimator shadowFadeIn;

    protected final int buttonBottomPadding;

    protected boolean shouldBeResponsive = true;
    protected boolean isShowing;

    protected RemoveView(Context context) {
        this.context = context;

        layout = (ComponentContainer) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_x_button_holder, null, false);
        button = layout.findComponentById(ResourceTable.Id_xButton);
        showLocation = (Text) layout.findComponentById(ResourceTable.Id_show_location);
        buttonImage = (Image) layout.findComponentById(ResourceTable.Id_xButtonImg);
        buttonBottomPadding = button.getPaddingBottom();
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColors(new RgbColor[]{RgbColor.fromArgbInt(0x30000000), RgbColor.fromArgbInt(0xFF000000)});
        shapeElement.setOrientation(ShapeElement.Orientation.TOP_TO_BOTTOM);
        shapeElement.setShaderType(LINEAR_GRADIENT_SHADER_TYPE);
        shadow = layout.findComponentById(ResourceTable.Id_shadow);
        shadow.setBackground(shapeElement);
        windowManager = WindowManager.getInstance();
        shadowFadeIn = SimpleAnimator.getInstance(buttonImage);
        shadowFadeOut = SimpleAnimator.getInstance(buttonImage);
    }

    protected void setIconResId(Element element) {
        buttonImage.setImageElement(element);
    }

    protected void setShadowBG(Element shadowBG) {
        shadow.setBackground(shadowBG);
    }

    public void show() {
        Log.i("RemoveView.show([]):68  startAnimation");
        if (layout != null && layout.getComponentParent() == null) {
            addToWindow(context, layout);
        }
        shadowFadeIn.setWindow(window);
        shadowFadeIn.setAnimationStateChangeListener(new MiStateChangeListener());
        shadowFadeIn.startAnimation(SimpleAnimator.IN);
    }

    public boolean isShowing() {
        return isShowing;
    }

    public Image getButtonImage() {
        return buttonImage;
    }

    public void hide() {
        shadowFadeOut.setAnimationStateChangeListener(new MiStateChangeListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                destroy();
            }
        });
        shadowFadeOut.setWindow(window);
        shadowFadeOut.startAnimation(SimpleAnimator.OUT);
    }


    protected void onMove(final float x, final float y) {
        if (shadowFadeIn.animatorValue.isRunning() || shadowFadeOut.animatorValue.isRunning()) {
            return;
        }
        try {
            if (shouldBeResponsive && button != null) {
                final int midpoint = getDisplayWidthInPx(button.getContext());
                final float xDelta = BigDecimal.valueOf(x).subtract(BigDecimal.valueOf(midpoint)).floatValue();
                final int xTransformed = (int) Math.abs(xDelta * 100 / midpoint);
                final int bottomPadding = buttonBottomPadding - (xTransformed / 5);
                if (xDelta < 0) {
                    button.setPadding(0, 0, xTransformed, bottomPadding);
                } else {
                    button.setPadding(xTransformed, 0, 0, bottomPadding);
                }
            }
        } catch (Exception e) {

        }
    }

    protected void destroy() {
        if (layout.getComponentParent() != null) {
            layout.getComponentParent().removeComponent(layout);
        }
        if (window != null && windowManager != null) {
            windowManager.destroyWindow(window);
            window = null;
        }
        isShowing = false;
    }

    private Window window;

    public Window getWindow() {
        return window;
    }

    private void addToWindow(Context context, ComponentContainer layout) {
        WindowManager.LayoutConfig params = new WindowManager.LayoutConfig();
        params.width = getDisplayWidthInPx(context);
        Log.i("RemoveView.addToWindow([context, layout]):140   " + getDisplayWidthInPx(context));
        params.height = AttrHelper.vp2px(160, context);
        params.x = 0;
        params.pixelFormat = -3;
        params.y = getDisplayHightInPx(context) - params.height;
        params.alignment = (1 | 2 << 4) | (1 | 2 << 0);
        window = WindowManager.getInstance().addComponent(layout, context, WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
        window.setLayoutConfig(params);
        window.setTransparent(true);
        isShowing = true;

    }
}
